package controllers;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Query;
import javax.transaction.Synchronization;

import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.libs.F;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import entities.History;
import entities.Objects;
import entities.Station;

/**
 * Created with IntelliJ IDEA.
 * User: eagami
 * Date: 03/05/14
 * Time: 16:03
 * To change this template use File | Settings | File Templates.
 */
public class HistoryController extends Controller {

	private static List<History> getHistory(){
		StringBuilder strQuery = new StringBuilder("select h ");
		   strQuery.append("from History h ");
		   
			Query query = JPA.em().createQuery(strQuery.toString());
			
			System.out.println(query.getResultList());
			
			return query.getResultList();
	}
	
    @Transactional(readOnly = true)
    public static Result getAllHistory(){
        return ok(Json.toJson(getHistory()));
    }
    
    public static void insertOrUpdateHistory(final Long stationId,final Integer tag,final int x,final int y) throws Throwable{
    	
    	JPA.withTransaction(new F.Callback0() {
	        @Override
	        public void invoke() throws Throwable {
    	
	        StringBuilder strQuery = new StringBuilder("select h ");
		   strQuery.append("from History h ");
		   strQuery.append("where h.tag.number = ?1 and ");
		   
		   	if(stationId == null){
		   		strQuery.append("h.station.id is null");
		   	}else{
		   		strQuery.append("h.station.id = ?2");
		   	}
		   
			Query query = JPA.em().createQuery(strQuery.toString());
			
			query.setParameter(1, tag);
			
			if(stationId != null)
				query.setParameter(2, stationId);
			
			//Insert
			if(query.getResultList().isEmpty()){
				StringBuilder strQueryObject = new StringBuilder("select o ");
		
				strQueryObject.append("from Objects o ");
				strQueryObject.append("where o.tag.number = ?1");
				   
					Query queryObject = JPA.em().createQuery(strQueryObject.toString());
					
					queryObject.setParameter(1, tag);
					
					List<Objects> lObject = queryObject.getResultList();
					if(!lObject.isEmpty()){
						
						History history = new History();
						
						history.setLastUpdateTime(new Date());
						history.setLocationX(new Integer(x));
						history.setLocationY(new Integer(y));
						history.setStartTime(new Date());
						history.setStation(Station.getStationByID(stationId));
					
						Objects o = lObject.get(0);
						history.setTag(o.getTag());
						history.setObject(o);
						JPA.em().persist(history);
					}
			}
			//Update
			else{
				List<History> l = query.getResultList();
				for (Iterator<History> iterator = l.iterator(); iterator.hasNext();) {
					try{
					History history = (History) iterator.next();
					history.setLastUpdateTime(new Date());
					history.setLocationX(new Integer(x));
					history.setLocationY(new Integer(y));
					JPA.em().merge(history);
					}catch(ClassCastException e){
						
					}
				}
	        }
    	}
	        
    	});
    }
}
